<template>
  <div class="leave-word">
    <div class="word-input-wrapper">
      <textarea
        class="word-input"
        ref="wordInput"
        placeholder="请输入留言..."
        v-model="words"
      ></textarea>
      <div class="emoji-bar">
        <button @click="chooseEmoji('[鲜花]')"></button>
        <button @click="chooseEmoji('[蜡烛]')"></button>
        <button @click="chooseEmoji('[绶带]')"></button>
        <button @click="chooseEmoji('[白鸽]')"></button>
      </div>
    </div>
    <br />
    <div class="send-btn" @click="send">提交</div>
  </div>
</template>

<script>
/* eslint-disable one-var */
import {LybSaveDjzxxDataOrUpdate} from '../assets/js/api';
export default {
  data() {
    return {
      id: this.$route.query.id,
      words: '',
      dspToken: localStorage.dspToken
    };
  },
  methods: {
    chooseEmoji(emoji) {
      let editor = this.$refs.wordInput;
      if (typeof editor.selectionStart === 'number' && typeof editor.selectionEnd === 'number') {
        let startPos = editor.selectionStart,
          endPos = editor.selectionEnd,
          cursorPos = startPos,
          tmpStr = editor.value;
        editor.value = tmpStr.substring(0, startPos) + emoji + tmpStr.substring(endPos, tmpStr.length);
        cursorPos += emoji.length;
        editor.focus();
        editor.selectionStart = editor.selectionEnd = cursorPos;
      } else {
        editor.value += emoji;
      }
    },
    ReplaceAll(str, sptr, sptr1) {
      while (str.indexOf(sptr) >= 0) {
        str = str.replace(sptr, sptr1);
      }
      return str;
    },
    send() {
      let words = this.words;
      if (!words) {
        this.$toast('请填写留言内容！');
        return;
      } else {
        words = this.ReplaceAll(words, '[', '&');
        words = this.ReplaceAll(words, ']', '&1');
      }
      LybSaveDjzxxDataOrUpdate({
        jbxxId: this.id,
        lynr: words,
        dspToken: this.dspToken
      }).then(res => {
        this.$toast('留言提交成功');
        this.$router.replace({name: 'MessageDetail', query: {id: this.id}});
      });
    }
  }
};
</script>

<style lang="less" scoped>
.leave-word {
  min-height: 100%;
  box-sizing: border-box;
  background: #f5f5f5;
  padding: 20px;
  box-sizing: border-box;
  .word-input-wrapper {
    position: relative;
    height: 428px;
    background: #fff;
    display: flex;
    flex-direction: column;
    background-color: #fff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    padding: 36px 36px 20px 36px;
    .word-input {
      flex: 1;
      margin-bottom: 10px;
      outline: none;
      font-size: 34px;
      border: none;
      color: #aaa;
    }
    .emoji-bar {
      display: flex;
      justify-content: space-around;
      height: 84px;
      > button {
        width: 50px;
        height: 50px;
        border: none;
        outline: none;
        appearance: none;
        &:nth-child(1) {
          background: url("../../static/images/emoji/flower.png") no-repeat
            center center;
          background-size: contain;
        }
        &:nth-child(2) {
          background: url("../../static/images/emoji/candle.png") no-repeat
            center center;
          background-size: contain;
        }
        &:nth-child(3) {
          background: url("../../static/images/emoji/scarves.png") no-repeat
            center center;
          background-size: contain;
        }
        &:nth-child(4) {
          background: url("../../static/images/emoji/pigeon.png") no-repeat
            center center;
          background-size: contain;
        }
      }
    }
  }
  .send-btn {
    height: 90px;
    line-height: 90px;
    text-align: center;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    font-size: 36px;
    letter-spacing: 7px;
    color: #fff;
  }
}
</style>
